package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsNarratorThemeDetail;
import com.macro.mall.dto.BmsNarratorThemeParam;
import com.macro.mall.mapper.BmsNarratorThemeMapper;
import com.macro.mall.model.BmsNarratorTheme;
import com.macro.mall.model.BmsNarratorThemeExample;
import com.macro.mall.service.BmsNarratorThemeService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class BmsNarratorThemeServiceImpl implements BmsNarratorThemeService {

    @Autowired
    private BmsNarratorThemeMapper bmsNarratorThemeMapper;

    @Autowired
    private UserUtils userUtils;

    @Override
    public List<BmsNarratorTheme> getList(BmsNarratorThemeParam bmsNarratorThemeParam) {
        if (Objects.isNull(bmsNarratorThemeParam.getNarratorId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsNarratorThemeParam.getPageNum();
        Integer pageSize = bmsNarratorThemeParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsNarratorThemeExample example = new BmsNarratorThemeExample();
        BmsNarratorThemeExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        criteria.andNarratorIdEqualTo(bmsNarratorThemeParam.getNarratorId());
        if (StringUtils.isNotBlank(bmsNarratorThemeParam.getName())) {
            criteria.andNameLike("%" + bmsNarratorThemeParam.getName() + "%");
        }
        if (Objects.nonNull(bmsNarratorThemeParam.getOpeningTime())) {
            criteria.andOpeningTimeGreaterThanOrEqualTo(bmsNarratorThemeParam.getOpeningTime());
        }
        if (Objects.nonNull(bmsNarratorThemeParam.getCloseingTime())) {
            criteria.andCloseingTimeLessThanOrEqualTo(bmsNarratorThemeParam.getCloseingTime());
        }
        example.setOrderByClause("create_time desc");
        List<BmsNarratorTheme> list = bmsNarratorThemeMapper.selectByExample(example);
        return list;
    }

    @Override
    public int create(BmsNarratorThemeParam bmsNarratorThemeParam) {
        BmsNarratorTheme bmsNarratorTheme = new BmsNarratorTheme();
        BeanUtils.copyProperties(bmsNarratorThemeParam, bmsNarratorTheme);
        Long userId = userUtils.getUserId();
        bmsNarratorTheme.setCreateUser(userId);
        bmsNarratorTheme.setUpdateUser(userId);
        bmsNarratorTheme.setCreateTime(new DateTime());
        bmsNarratorTheme.setUpdateTime(new DateTime());
        int count = bmsNarratorThemeMapper.insertSelective(bmsNarratorTheme);
        return count;
    }

    @Override
    public int update(BmsNarratorThemeParam bmsNarratorThemeParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsNarratorTheme bmsNarratorTheme = new BmsNarratorTheme();
        bmsNarratorTheme.setId(bmsNarratorThemeParam.getId());
        BeanUtils.copyProperties(bmsNarratorThemeParam, bmsNarratorTheme);
        bmsNarratorTheme.setUpdateTime(new DateTime());
        bmsNarratorTheme.setUpdateUser(userId);
        int conut = bmsNarratorThemeMapper.updateByPrimaryKeySelective(bmsNarratorTheme);

        return conut;
    }

    @Override
    public BmsNarratorThemeDetail getItem(Long id) {

        BmsNarratorTheme bmsNarratorTheme = bmsNarratorThemeMapper.selectByPrimaryKey(id);
        BmsNarratorThemeDetail detail = new BmsNarratorThemeDetail();
        BeanUtils.copyProperties(bmsNarratorTheme, detail);
        return detail;
    }

    @Override
    public int delete(List<Long> ids) {
        BmsNarratorThemeExample example = new BmsNarratorThemeExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        BmsNarratorTheme updateDetail = new BmsNarratorTheme();
        updateDetail.setUpdateUser(userId);
        updateDetail.setUpdateTime(new Date());
        updateDetail.setIsDelete((byte) 1);
        return bmsNarratorThemeMapper.updateByExampleSelective(updateDetail,example);
    }

    @Override
    public List<BmsNarratorTheme> findAllList() {
        BmsNarratorThemeParam query = new BmsNarratorThemeParam();
        return bmsNarratorThemeMapper.findList(query);
    }

}


